package helloworld.admin.form.center;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.ImageButtonBean;
import org.apache.struts.util.MessageResources;
import helloworld.example.util.BaseActionForm;


/**
 * CenterForm is the object representation of the html form for entering
 * center's name in helloworld.
 * 
 * @author TUYEN-DM
 * @version $Revision: $ $Date: 20090503 $
 */
public class CenterForm extends BaseActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Center Name */
	private String centerName;
	private String email;
	private String phone;
	private String address;
	private int directorID;
	private String openDate;
	private String website;
	
	private boolean isExisting;

	private int centerID;

	private ImageButtonBean save;
	private ImageButtonBean cancel;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getDirectorID() {
		return directorID;
	}

	public void setDirectorID(int directorID) {
		this.directorID = directorID;
	}

	public String getOpenDate() {
		return openDate;
	}

	public void setOpenDate(String openDate) {
		this.openDate = openDate;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setExisting(boolean isExisting) {
		this.isExisting = isExisting;
	}

	public CenterForm() {
		// Dont forget to Initialize here
		// Also dont forget to init in reset();
		init();
	}

	// Without this method to initialization, non-primitive objects such as
	// Address, ImageButtonBean will throw NullPointerException
	// or might not work as expected
	// since a getAddress(), getSave() etc. returns a null object
	protected void init() {
		centerName = "";
		phone = "";
		openDate = "";
		directorID = 0;
		email = "";
		address = "";
		website = "";
		save = new ImageButtonBean();
		cancel = new ImageButtonBean();
		isExisting = false;
		centerID = 0;
	}

	public int getCenterID() {
		return centerID;
	}

	public void setCenterID(int i) {
		centerID = i;
	}

	public String getCenterName() {
		return centerName;
	}

	public void setCenterName(String s) {
		this.centerName = s;
	}

	public String getEmailAddress() {
		return email;
	}

	public void setEmailAddress(String string) {
		email = string;
	}

	public String getAddress() {
		return address;
	}

	public ImageButtonBean getCancel() {
		return cancel;
	}

	public ImageButtonBean getSave() {
		return save;
	}

	public boolean getIsExisting() {
		return isExisting;
	}

	public void setIsExisting(boolean b) {
		isExisting = b;
	}

	/**
	 * This method is called by the RequestProcessor immediately after
	 * populating the CenterForm only if the validate=true flag is set in the
	 * helloworld-config.xml Do validation here and return ActionErrors if any,
	 * to the RequestProcessor
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		// Need to do this explicitly now. Since the Button is not grey and its
		// name is not
		// org.apache.struts...Cancel anymore, and the validate=true still
		// remains in ActionMapping, the call to bypass validation on clicking
		// cancel image
		// has to be performed manually
		if (getCancel().isSelected()) {
			return errors;
		}

		MessageResources msgRes = (MessageResources) request
				.getAttribute(Globals.MESSAGES_KEY);

		// CenterName cannot be empty
		if (centerName == null || centerName.trim().equals("")) {
			String centerName = msgRes.getMessage("center.centername");
			String[] rplcmntValueArr = { centerName };
			ActionError err = new ActionError("error.required", rplcmntValueArr);
			errors.add("centerName", err);
		}
		return errors;
	}

}